package com.github.zdg.recyclerview;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by zoudong on 2017/3/6.
 */
public abstract class BaseRecyclerAdapter<VH extends BaseRecyclerAdapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    protected final Context mContext;
    public static final int UNKNOWN_VIEW_TYPE = Integer.MIN_VALUE;
    protected ViewHolderHelper<BaseRecyclerAdapter.ViewHolder> mHeaderViewHolder = new ViewHolderHelper<>();
    protected ViewHolderHelper<BaseRecyclerAdapter.ViewHolder> mFooterViewHolder = new ViewHolderHelper<>();
    protected ViewHolderHelper<ViewHolderFactory> mFactoryViewHolderHelper = new ViewHolderHelper<>();

    BaseRecyclerAdapter(Context mContext) {
        this.mContext = mContext;
    }

    protected BaseRecyclerAdapter.ViewHolder getHeadOrFooterViewHolder(int type) {
        if (mHeaderViewHolder.get(type) != null) {
            return mHeaderViewHolder.get(type);
        }
        return mFooterViewHolder.get(type);
    }

    protected static abstract class ViewHolder<T> extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        protected Context mContext;

        protected ItemOnClickListenter<T> mOnClickListenter;
        protected ItemOnLongClickListenter<T> mLongClickListenter;

        protected T data;
        private int position;
        private ViewHolderHelper mHeader, mFooter;

        ViewHolder(@NonNull View item) {
            super(item);
            mContext = item.getContext();
            itemView.setOnClickListener(this);
            itemView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (mOnClickListenter != null) {
                mOnClickListenter.onItemClick(ViewHolder.this, data, getAdapterPosition() - mHeader.size());
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (mLongClickListenter != null) {
                mLongClickListenter.onItemLongClick(ViewHolder.this, data, getAdapterPosition() - mHeader.size());
            }
            return true;
        }

        public void setData(T data) {
            this.data = data;
        }

        /**
         * @param item
         * @param position
         * @param onClickListenter
         * @param longClickListenter
         * @param headerViewHolder   动态变化， 所以需要动态获取， 否则 position 异常
         * @param footerViewHolder
         */
        void convert(T item, int position, ItemOnClickListenter<T> onClickListenter, ItemOnLongClickListenter<T> longClickListenter, ViewHolderHelper<ViewHolder> headerViewHolder, ViewHolderHelper<ViewHolder> footerViewHolder) {
            this.data = item;
            this.position = position;
            this.mHeader = headerViewHolder;
            this.mFooter = footerViewHolder;
            this.mOnClickListenter = onClickListenter;
            this.mLongClickListenter = longClickListenter;
            onBindData(item, position);
        }

        public abstract void onBindData(T item, int position);

    }

    public void addViewType(int key, ViewHolderFactory factory) {
        mFactoryViewHolderHelper.addViewHolder(key, factory);
    }

    public interface ViewHolderFactory<VH extends ViewHolder> {

        VH onCreateViewHolder(ViewGroup parent);
    }
}
